package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.ListUtil;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.basic.sys.dao.SysLoginLogMapper;
import com.swiftops.basic.sys.model.SysLoginLog;
import com.swiftops.basic.sys.service.SysLoginLogService;
import com.swiftops.basic.sys.vo.entity.SysLoginLogEntityVO;
import com.swiftops.basic.sys.vo.request.SysLoginLogRequestVO;
import com.swiftops.basic.sys.vo.response.SysLoginLogResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 登录日志
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysLoginLogServiceImpl extends AbstractService<SysLoginLog> implements SysLoginLogService {

    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    /**
     * 获取登录日志分页列表
     *
     * @param sysLoginLogRequestVO 登录日志查询条件
     * @return pageResult 登录日志分页结果
     */
    @Override
    public PageResult<SysLoginLogResponseVO> querySysLoginLogPageList(SysLoginLogRequestVO sysLoginLogRequestVO) {
        //
        if (sysLoginLogRequestVO.getPageNumber() == null) sysLoginLogRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysLoginLogRequestVO.getPageSize() == null) sysLoginLogRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysLoginLogResponseVO> sysLoginLogResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysLoginLog.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysLoginLogRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysLoginLogRequestVO.getDesc() + "%");
        //}
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysLoginLogRequestVO.getPageNumber(), sysLoginLogRequestVO.getPageSize());
            List<SysLoginLog> sysLoginLogList = this.selectByCondition(condition);
            sysLoginLogResponseVOList = ListUtil.convertList(sysLoginLogList, SysLoginLogResponseVO.class);
        }
        //
        return PageResult.of(total, sysLoginLogResponseVOList);
    }

    /**
     * 获取登录日志详细信息
     *
     * @param id 登录日志ID
     * @return 登录日志
     */
    @Override
    public SysLoginLogEntityVO getSysLoginLogEntityVO(String id) {
        SysLoginLog sysLoginLog = this.selectByPrimaryKey(id);
        if (null == sysLoginLog) return null;

        SysLoginLogEntityVO sysLoginLogEntityVO = new SysLoginLogEntityVO();
        BeanUtils.copyProperties(sysLoginLog, sysLoginLogEntityVO);
        return sysLoginLogEntityVO;
    }

    /**
     * 添加登录日志
     *
     * @param sysLoginLogEntityVO 登录日志
     */
    @Override
    public void addSysLoginLog(SysLoginLogEntityVO sysLoginLogEntityVO) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        BeanUtils.copyProperties(sysLoginLogEntityVO, sysLoginLog);
        sysLoginLog.setId(IdGen.next());
        this.insertSelective(sysLoginLog);
    }

    /**
     * 修改登录日志
     *
     * @param sysLoginLogEntityVO 登录日志
     */
    @Override
    public void updateSysLoginLog(SysLoginLogEntityVO sysLoginLogEntityVO) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        BeanUtils.copyProperties(sysLoginLogEntityVO, sysLoginLog);
        this.updateByPrimaryKeySelective(sysLoginLog);
    }


    /**
     * 删除登录日志
     *
     * @param id 登录日志ID
     */
    @Override
    public void deleteSysLoginLog(String id) {
        this.deleteByPrimaryKey(id);
    }
}
